from excel.service.data import excel_data
from mailmerge import MailMerge
"""
pip install docx-mailmerge
1、设置域：插入->文档部件->域->邮件合并->输入域代码（例：MERGEFIELD 姓名）
2、字典键与域名一一对应，自动填入值
"""
excel_path = r"resource/data.xlsx"
doc_path = r"resource/template.docx"
e = excel_data().load(excel_path).read()
for col in e.main:
    col = [str(i) for i in col] # 转为字符串
    col = e.format(col) # 格式化列数据
    doc = MailMerge(doc_path)
    # doc.merge(_calss=col["班级"],name=col["姓名"], id=str(col["序号"]))
    doc.merge(**col)    # 等价于上
    doc.write(rf"resource/{col['姓名']}.docx")